package com.viewplug.view.custom;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;

import com.viewplug.R;
import com.viewplug.view.utils.ScreenUtils;

/**
 * Created by mac_wk on 14-12-30.
 */
//仿qq切图
public class QQSlice extends View {
    public QQSlice(Context context) {
        this(context, null);
    }

    public QQSlice(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public QQSlice(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public QQSlice(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);
    }

    private Bitmap slice_bp;
    private Canvas canvas;
    private Bitmap slice_source;
    private PorterDuffXfermode porterDuffXfermode_dst_out;
    private float ref_width = 280f;
    private Rect rect = null;
    private GestureDetector mGestureDetector;
    private void init() {
        slice_source = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        int tb = 10;
        rectF = new RectF();
        rect  = new Rect();
        rect.set(0,0,100,100);
        int width = ScreenUtils.getScreenWidth(getContext());
        int height = ScreenUtils.getScreenHeight(getContext());
        float start = width / 2 - ref_width / 2;
        float end = height / 2 - ref_width / 2;
        rectF.set(start, end, start + ref_width, end + ref_width);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setTextSize(tb * 6.0f);
        paint.setStrokeWidth(tb * 0.2f);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setStyle(Paint.Style.STROKE);
    }

    private RectF rectF = null;
    private Paint paint;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    private int top;
    private int left;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(slice_source, rect, rectF, null);
        canvas.drawArc(rectF, 0, 360, false, paint);
    }

   private PointF pointF = new PointF();
    private final static String TAG="slice";
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                pointF.set(event.getX(),event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                int x = (int) (event.getX() - pointF.x);
                int y = (int) (event.getY() - pointF.y);
                top+=y;
                left+=x;
                invalidate();
                postInvalidate();
                Log.d(TAG,""+top);
                pointF.set(event.getX(),event.getY());
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}
